<?php

/**
 * OrderController.class.php
 * @package Controller
 * @version $Id: IndexController.class.php 1.0 2016-11-07 22:18:13Z $
 */
class UserController extends BaseController
{
    /**
     * 前函数
     */
    public function init()
    {
        parent::init();
        if (LoginHelper::getUserName() == '' || LoginHelper::getUserId() == 0) {
            $this->redirect($this->_createUrl('login/index'));
        }
        return true;
    }

    /**
     * 用户列表
     */
    public function userListAction()
    {
        $model = Controller::createMode(BaseController::$default_db, 't_26_yonghuliebiao');
        $map = '1=1';
        $account_name = $this->get('account_name');
        if ($account_name!='') {
            $map = 'mobile like "%' . $account_name . '%"';
        }
        $pagesize = 20;
        $page = $_GET['page']; // get the requested page
        if (!$page) {
            $start = 0;
        } else {
            $start = ($page - 1) * $pagesize;
        }

        $screen_str = '';
        if ($account_name!='') {
            $screen_str .= "account_name=" . $account_name . "&";
        }
        $count = $model->count('t_26_yonghuliebiao', 1, $map);
        $page = (int)$this->get('page', 1);
        $pager = $this->instance('pager');
        $pager->loadCss('classic');
        $params['w'] = 1;
        $page_url = $this->_createUrl('user/userlist', $params) . "?" . $screen_str . "page=";
        $pager_html = $pager->setIsGoNode(false)->total($count)->num($pagesize)->page($page)->url($page_url)->output();

        $list = $model->where($map)->limit($start, $pagesize)->select();
        $model_tag = Controller::createMode(BaseController::$default_db, 't_26_adm_user_tags');
        foreach ($list as $key => $val) {
            $tag = $model_tag->getOne(array('user_id' => $val['user_id']));
            $list[$key]['tag_name'] = $tag['tag_names'];
            $list[$key]['type_name'] = '<span class="label label-lg label-pink arrowed-right">普通会员</span>';
            if ($val['type'] == 2) {
                $list[$key]['type_name'] = '<span class="label label-danger arrowed-in">银牌会员</span>';
            } elseif ($val['type'] == 3) {
                $list[$key]['type_name'] = '<span class="label label-lg label-yellow arrowed-in">金牌会员</span>';
            }
            $list[$key]['gender_name'] = '男';
            if ($val['gender'] == 2) {
                $list[$key]['gender_name'] = '女';
            }
        }
        $this->assign('list', $list);
        $this->assign('pager_html', $pager_html);
        $this->display();
    }

    /**
     *  修改标签名称
     */
    public function editRealNameAction()
    {
        $id = $this->post('user_id');
        $real_name = $this->post('real_name');
        $model = Controller::createMode(BaseController::$default_db, 't_26_yonghuliebiao');
        if ($id) {
            if ($model->update(array('real_name' => $real_name), array('user_id' => $id))) {
                echo json_encode(array('code' => 200, 'msg' => '修改成功'));
                exit();
            }
        }
        echo json_encode(array('code' => 201, 'msg' => '发生未知错误，修改失败！'));
        exit();
    }

    /**
     * 获取标签列表
     */
    public function getTagListAction()
    {
        $id = $this->post('id');
        $opt = $this->post('opt');
        // 添加，修改操作
        if ($opt == 'edit') {
            $model = Controller::createMode(BaseController::$default_db, 't_26_adm_user_tags');
            if ($model->getOne(array('user_id' => $id))) {
                if ($model->update(array('tag_names' => $this->post('str')), array('user_id' => $id))) {
                    echo json_encode(array('code' => 200, 'msg' => '修改成功'));
                    exit();
                }
                echo json_encode(array('code' => 201, 'msg' => '修改失败'));
                exit();
            } else {
                if ($model->insert(array('user_id' => $id, 'tag_names' => $this->post('str')))) {
                    echo json_encode(array('code' => 200, 'msg' => '添加成功'));
                    exit();
                }
                echo json_encode(array('code' => 201, 'msg' => '添加失败'));
                exit();
            }
            // 获取用户已存在的标签
        } else if ($opt == 'Tag') {
            $model = Controller::createMode(BaseController::$default_db, 't_26_adm_user_tags');
            $tag = $model->getOne(array('user_id' => $id));
            if (!empty($tag)) {
                $tag = explode(',', $tag['tag_names']);
                echo json_encode(array('code' => 200, 'msg' => '数据获取成功', 'data' => $tag));
                exit();
            }
            echo json_encode(array('code' => 201, 'msg' => '出错'));
            exit();
            // 获取标签列表
        } else {
            $model = Controller::createMode(BaseController::$default_db, 't_26_adm_user_tag_list');
            $list = $model->select();
            if (!empty($list)) {
                echo json_encode(array('code' => 200, 'data' => $list));
                exit();
            }
            echo json_encode(array('code' => 201, 'msg' => '数据获取失败'));
            exit();
        }
    }

   

    public function passwordSetAction(){
        $model = Controller::createMode(BaseController::$default_db, 't_26_yonghuliebiao');
        $user = $model->getOne(array('user_id'=>$this->get('id')));
        if($_POST){
            $data['mobile']=$this->post('mobile');
            $data['state']=$this->post('state');
            if($this->post('password')){
                $data['password']=md5(trim($this->post('password')));
            }
            if($model->update($data,array('user_id'=>$this->get('id')))){
                echo 200;exit;
            }else{
                echo 201;exit;
            }
        }else{
            $this->assign('user',$user);
            $this->display('password_set');
        }
    }

    /**
     * 标签操作 增删改
     */
    public function tagOptionAction()
    {
        $opt = $this->post('opt');
        $id = $this->post('id');
        $model = Controller::createMode(BaseController::$default_db, 't_26_adm_user_tag_list');
        if ($opt == 'add') {
            $arr = array(
                'order_id' => $this->post('order_id'),
                'kind_name' => $this->post('kind_name'),
                'image' => $this->post('image'),
                'remark' => $this->post('remark'),
                'parent_id' => $id,
            );
            if ($model->insert($arr)) {
                echo json_encode(array('code' => 200, 'msg' => '添加成功'));
                exit();
            }
            echo json_encode(array('code' => 201, 'msg' => '添加失败'));
            exit();
        } elseif ($opt == 'del') {
            if ($model->delete(array('kind_id' => $id))) {
                echo json_encode(array('code' => 200, 'msg' => '删除成功'));
                exit();
            }
            echo json_encode(array('code' => 201, 'msg' => '删除失败'));
            exit();
        } elseif ($opt == 'edit') {
            $arr = array(
                'order_id' => $this->post('order_id'),
                'kind_name' => $this->post('kind_name'),
                'image' => $this->post('image'),
                'remark' => $this->post('remark'),
            );
            if ($model->update($arr, array('kind_id' => $id))) {
                echo json_encode(array('code' => 200, 'msg' => '修改成功'));
                exit();
            }
            echo json_encode(array('code' => 201, 'msg' => '修改失败'));
            exit();
        } elseif ($opt == 'getTag') {
            $list = $model->getOne(array('kind_id' => $id));
            echo json_encode(array('code' => 200, 'msg' => '成功', 'data' => $list));
            exit();
        }
    }

    /**
     * 标签增删改
     */
    public function tagEditAction()
    {
        $opt = $this->post('opt');
        $kind_id = $this->post('kind_id');
        $model = Controller::createMode(BaseController::$default_db, 't_26_adm_user_tag_list');
        if ($opt == 'add') {
            $kind_name = $this->post('kind_name');
            if ($model->getOne(array('kind_name' => $kind_name, 'parent_id' => $kind_id))) {
                echo json_encode(array('code' => 201, 'msg' => '该标签已存在，不能重复添加'));
                exit();
            }
            $insert = $model->insert(array('kind_name' => $kind_name, 'parent_id' => $kind_id), true);
            if ($insert) {
                echo json_encode(array('code' => 200, 'msg' => '添加成功', 'kind_id' => $insert));
                exit();
            }
            echo json_encode(array('code' => 201, 'msg' => '添加失败', 'kind_id' => $insert));
            exit();
        } elseif ($opt == 'upd') {
            if ($model->update(array('kind_name' => $this->post('kind_name')), array('kind_id' => $kind_id))) {
                echo json_encode(array('code' => 200, 'msg' => '修改成功'));
                exit();
            }
            echo json_encode(array('code' => 201, 'msg' => '修改失败'));
            exit();
        } elseif ($opt == 'del') {
            if ($model->getOne(array('parent_id' => $kind_id))) {
                echo json_encode(array('code' => $kind_id, 'msg' => '该分类下有子分类，无法删除'));
                exit();
            } elseif ($model->delete(array('kind_id' => $kind_id))) {
                echo json_encode(array('code' => 200, 'msg' => '删除成功'));
                exit();
            }
            echo json_encode(array('code' => 201, 'msg' => '删除失败'));
            exit();
        }
    }

    public function K3PeijiankuanAction(){

        $username = $this->get('name');
        if($username !== ''){
            $model = Controller::createMode(BaseController::$default_db, 't_26_yonghuliebiao');
            $sql = "select * from t_26_yonghuliebiao where `name` like '%".$username."%'";
            $data = $model->execute($sql);
            if($data){
                echo json_encode($data);
            }else{
                echo json_encode(array('errorCode'=>100));
            }

        }


        //$this->assign('');
        $this->display('peijiankuan');
    }
}
